import { Directive, Input } from '@angular/core';
import { AbstractControl, NG_VALIDATORS, Validator } from '@angular/forms';

import { ValidatorFactoryService } from '../../../core/services/validator-factory.service';

@Directive({
    selector: '[forbiddenValue]',
    providers: [
        {
            provide: NG_VALIDATORS,
            useExisting: ForbiddenValueDirective,
            multi: true
        }
    ]
})
export class ForbiddenValueDirective implements Validator {
    @Input() forbiddenValue: string;

    constructor(private validatorFactory: ValidatorFactoryService) { }

    validate(control: AbstractControl): { [key: string]: any } {
        return this.forbiddenValue ? this.validatorFactory.getForbiddenValueValidator(new RegExp(this.forbiddenValue, 'i'))(control) : null;
    }
}